package com.action;

import com.entity.*;
import com.service.FilmService;
import com.service.HallService;
import com.service.RoundService;
import com.util.PageHelper;
import com.util.VeDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.stream.Collectors;

//场次管理
@Controller
// 设置路径
@RequestMapping(value = "/round", produces = "text/plain;charset=utf-8")
public class RoundAction extends BaseAction {

    @Autowired
    private RoundService roundService;
    @Autowired
    private FilmService filmService;
    @Autowired
    private HallService hallService;
    
    // 准备添加数据
    @RequestMapping("createRound.action")
    public String createRound() {

        List<Film> allFilm = filmService.getAllFilm();
        List<Hall> allHall = hallService.getAllHall();
        this.getRequest().setAttribute("filmList", allFilm);
        this.getRequest().setAttribute("hallList",allHall);
        return "admin/addround";

    }

    // 添加数据
    @RequestMapping("addRound.action")
    public String addRound(Round round) {
        this.roundService.insertRound(round);
        return "redirect:/round/getAllRound.action";
    }

    // 通过主键删除数据
    @RequestMapping("deleteRound.action")
    public String deleteRound(String id) {
        this.roundService.deleteRound(id);
        return "redirect:/round/getAllRound.action";
    }

    // 批量删除数据
    @RequestMapping("deleteRoundByIds.action")
    public String deleteRoundByIds() {
        String[] ids = this.getRequest().getParameterValues("roundid");
        for (String roundid : ids) {
            this.roundService.deleteRound(roundid);
        }
        return "redirect:/round/getAllRound.action";
    }

    // 更新数据
    @RequestMapping("updateRound.action")
    public String updateRound(Round round) {
        roundService.updateRound(round);
        return "redirect:/round/getAllRound.action";
    }

    // 显示全部数据
    @RequestMapping("getAllRound.action")
    public String getAllRound(String number) {
        List<Round> roundList = this.roundService.getAllRound();
        PageHelper.getPage(roundList, "round", null, null, 10, number, this.getRequest(), null);
        return "admin/listround";
    }

   /* // 按条件查询数据 (模糊查询)
    @RequestMapping("queryRoundByCond.action")
    public String queryRoundByCond(String cond, String name, String number) {

        Round round = new Round();
        if (cond != null) {
            if ("roundid".equals(cond)) {
                round.setRoundid(name);
            }
            if ("roundname".equals(cond)) {
                round.setRoundname(name);
            }
        }

        List<String> nameList = new ArrayList<String>();
        List<String> valueList = new ArrayList<String>();
        nameList.add(cond);
        valueList.add(name);
        PageHelper.getPage(this.roundService.getRoundById(round), "round", nameList, valueList, 10, number, this.getRequest(), "query");
        name = null;
        cond = null;
        return "admin/queryround";

    }*/

    // 按主键查询数据
    @RequestMapping("getRoundById.action")
    public String getRoundById(String id) {

        Round round = this.roundService.getRoundById(id);
        List<Film> allFilm = filmService.getAllFilm();
        List<Hall> allHall = hallService.getAllHall();
        this.getRequest().setAttribute("filmList", allFilm);
        this.getRequest().setAttribute("hallList",allHall);
        this.getRequest().setAttribute("round",round);

        return "admin/editround";

    }

    // 商品详情
    @RequestMapping("getRounds.action")
    public String getRound(String filmid,String hallid,String cinemaid,String ownday) {

        //日期列表
        Round round = new Round();
        round.getHall().setHallid(hallid);
        round.getFilm().setFilmid(filmid);
        round.getHall().getCinema().setCinemaid(cinemaid);
        if(ownday!=null && ""!=ownday) {
            round.setOwnday(VeDate.getStringToDate(ownday));
        }

        List<Round> rounds = roundService.getRoundByCond(round);

        this.getRequest().setAttribute("rounds",rounds);

        return "users/round";

    }
    // 选座
    @RequestMapping("getChoice.action")
//    @ResponseBody
    public String getChoice(String roundid) {

        if (this.getSession().getAttribute("userid") == null) {
            return "redirect:/index/preLogin.action";
        }

        Round round = roundService.getRoundById(roundid);
        Film film = filmService.getFilmById(round.getFilm().getFilmid());
        //获取座次
        List<Seats> seats = roundService.getrSeat(roundid);

        this.getRequest().setAttribute("round",round);
        this.getRequest().setAttribute("film",film);
        this.getRequest().setAttribute("seats",seats);

        return "users/choice";

    }

}